<?php

// .-----------------------------------------------------------------------------------
// |  Software: [HDPHP framework]
// |   Version: 2013.01
// |      Site: http://www.hdphp.com
// |-----------------------------------------------------------------------------------
// |    Author: 向军 <houdunwangxj@gmail.com>
// | Copyright (c) 2012-2013, http://houdunwang.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
// |   License: http://www.apache.org/licenses/LICENSE-2.0
// '-----------------------------------------------------------------------------------

/**
 * mysql方式处理SESSION
 * @package     session
 * @subpackage  driver
 * @author      后盾向军 <houdunwangxj@gmail.com>
 */
class MysqlSession extends SessionAbstract {

    private $db; //数据库连接对象
    private $link; //Mysql数据库连接
    private $sessionTable; //SESSION表

    /**
     * 构造函数
     */

    public function __construct() {

    }

    /**
     * session_start()时执行
     * @return boolean
     */
    public function open() {
        //得到带表前缀的SESSION数据表
        $sessionTable = C("SESSION_TABLE_NAME");
        $dbPrefix = C("DB_PREFIX");
        if ($dbPrefix) {
            $this->sessionTable = stripos($sessionTable, C("DB_PREFIX")) === 0 ? $sessionTable : C("DB_PREFIX") . $sessionTable;
        } else {
            $this->sessionTable = $sessionTable;
        }
        $link = mysql_connect(C("DB_HOST") . ':' . C("DB_PORT"), C("DB_USER"), C("DB_PASSWORD")); //连接Mysql
        $selectDatabase = mysql_select_db(C("DB_DATABASE"), $link); //选择数据库
        if (!$link || !$selectDatabase) {
            return false;
        }
        $this->link = $link;
        return true;
    }

    /**
     * 卸载SESSION
     * @param type $id
     * @return type
     */
    public function destroy($id) {
        $sql = "DELETE FROM " . $this->sessionTable . " WHERE sessid='$id'";
        mysql_query($sql, $this->link);
        return mysql_affected_rows($this->link) ? true : false;
    }

    /**
     * SESSION垃圾处理
     * @return boolean
     */
    public function gc() {
        $sql = "DELETE FROM " . $this->sessionTable . " WHERE atime<" . (NOW - $this->sessionLifeTime);
        mysql_query($sql, $this->link);
    }

    /**
     * 读取SESSION 数据
     * @param string $id
     * @return string
     */
    public function read($id) {
        $sql = "SELECT data FROM " . $this->sessionTable . " WHERE sessid='$id' AND " .
                "card ='" . $this->card . "' AND atime>" . (NOW - $this->sessionLifeTime);
        $result = mysql_query($sql, $this->link);
        if ($result) {
            $data = mysql_fetch_assoc($result);
            return $data['data'];
        }
        return '';
    }

    /**
     * 写入SESSION
     * @param type $id
     * @param type $data
     */
    public function write($id, $data) {
        $card = $this->card; //SESSION令牌
        $atime = NOW;
        $ip = ip_get_client();
        $sql = "REPLACE INTO " . $this->sessionTable . "(sessid,card,data,atime,ip)
            VALUES('$id','$card','$data',$atime,'$ip')";
        mysql_query($sql, $this->link);
        return mysql_affected_rows($this->link) ? true : false;
    }

    //关闭SESSION
    public function close() {
        //执行父类close触发垃圾处理
        parent::close();
        //关闭数据库连接
        mysql_close($this->link);
    }

}

?>
